/*
Copyright (c)2009 Seesmic, Inc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.seesmic.twhix
{
	
	public class Stanza
	{
		public var conn:Object;
		public var xml:XML;
		public var ns:String;
		public var tagName:String;
		public var parent:Stanza;
		public var p:Object = new Object;
		
		public function Stanza(connection:Object, parent:Stanza=null)
		{
			this.conn = connection;
			this.parent = parent;
		}
		
		public function fromXML(inxml:XML):void {
			this.xml = inxml;
			var tmp:Object;
			if(!parent) {
				for(var xpath:String in conn.stanzas) {
					if(conn.XPathMatch(inxml, xpath)) {
						tmp = new conn.stanzas[xpath](conn, this);
						tmp.fromXML(inxml);
						p[tmp.plugin_name] = tmp;
					}
				}
			}
			// override and populate internal values
			// process internal stanzas
		}
		
		public function getXML():XML {
			return this.xml;
		}
		
		public function hasPlugin(sub:String):Boolean {
			return p.hasOwnProperty(sub);
		}
		
		public function update():void {
			// re-create this.xml based on internal flags/objects
		}
		
		public function noHandler():void {
			// what do you do when this stanza isn't handled?
		}
		
		public function render():void {
			// resets this.xml
		}
		
		public function send():void {
			render();
			conn.send(xml.toString());
		}
		
		public function makeReply():Stanza {
			return this;
		}
		
		public function handled():void {
		}
		
	}
}
